﻿using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Hrm.Core;
using Hrm.EntityFramework;
using Hrm.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Hrm.Web.Framework
{
    public class EngineContext
    {
        public static IContainer IocContext { get; private set; }

        public static void Ioc()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<AppDomainTypeFinder>().As<ITypeFinder>().SingleInstance();
            builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().SingleInstance();
            builder.RegisterType<HrmDbContext>().As<IDbContext>().Named<IDbContext>("HrmDb").SingleInstance();
            builder.RegisterType<NoteDbContext>().As<IDbContext>().Named<IDbContext>("NoteDb").SingleInstance();
            builder.RegisterType<PermissionContext>().As<IDbContext>().Named<IDbContext>("Permission").SingleInstance();
            builder.RegisterType<StudioDbContext>().As<IDbContext>().Named<IDbContext>("StudioDb").SingleInstance();
            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("HrmDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("HrmDb"))
                .AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("NoteDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("NoteDb"))
                .AsImplementedInterfaces();

            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("PermissionDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("Permission"))
                .AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("StudioDbRepository")).WithParameter(ResolvedParameter.ForNamed<IDbContext>("StudioDb"))
                .AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
                .Where(t => t.Name.EndsWith("Service"))
                .AsImplementedInterfaces();
            builder.RegisterControllers(AppDomain.CurrentDomain.GetAssemblies());
            //builder.RegisterControllers(Assembly.GetExecutingAssembly());
            var container = builder.Build();
            var resolver = new AutofacDependencyResolver(container);
            DependencyResolver.SetResolver(resolver);
            IocContext = container;
        }

        public static void LogThreadStart()
        {
            var logService = IocContext.Resolve<ILogService>();
            ThreadPool.QueueUserWorkItem(o =>
            {
                while (true)
                {
                    if (GlobalQueue.LogQueue.Count > 0)
                    {
                        Log log = GlobalQueue.LogQueue.Dequeue();
                        if (log != null)
                        {
                            logService.Log(log);
                        }
                        else
                        {
                            Thread.Sleep(60 * 1000);
                        }
                    }
                    else
                    {
                        Thread.Sleep(60 * 1000);
                    }
                }
            });
        }
    }
}
